<template>
  <div class="dashboard-container car-models customer-list-box">
    <div class="app-container">
      <el-card class="table-card-box" shadow="never">
        <el-button type="warning" @click="handleAddSchedule" width="300px">添加工作模式</el-button>
      </el-card>
      <el-card class="table-card-box" shadow="never">
        <el-table :data="scheduleList" style="width: 100%">
          <el-table-column prop="id" label="工作模式编号" min-width="200px"></el-table-column>
          <el-table-column prop="name" label="工作模式名称" min-width="140px"></el-table-column>
          <el-table-column prop="workPatternTypeDesc" label="类型" min-width="100px"></el-table-column>
          <el-table-column prop="workDate" label="工作日期" min-width="460px"></el-table-column>
          <el-table-column prop="duration" label="工作时长" min-width="130px"></el-table-column>
          <el-table-column label="操作" min-width="120px">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="handleEditSchedule(scope.row)"
                class="customer-add-btn"
                style="color:#419EFF;border: none;padding: 0; margin: 0;background-color:transparent;"
              >编辑
              </el-button>
              <span class="line"></span>
              <el-button
                type="text"
                @click="handleDeleteSchedule(scope.row) "
                class="customer-add-btn"
                style="border: none;padding: 0; margin: 0;background-color:transparent;color:#F56C6C;"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
        <workArrangeSettingAdd ref="workArrangeSettingAdd" @refresh="fetchScheduleList" />
      </el-card>
    </div>
  </div>
</template>

<script>
import { delWorkPattern, getWorkPattern } from '@/api/vehicle'

import workArrangeSettingAdd from './components/workArrange-setting-add.vue'

export default {
  components: { workArrangeSettingAdd },
  data() {
    return {
      scheduleList: [], // 排班列表数据
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页显示条数
      total: 0 // 总条数
    }
  },
  mounted() {
    this.fetchScheduleList()
  },
  methods: {
    // 处理添加工作模式
    handleAddSchedule() {
      this.$refs.workArrangeSettingAdd.showDialog()
    },
    // 处理编辑工作模式
    handleEditSchedule(schedule) {
      // 打开编辑排班的弹窗或跳转到编辑排班的页面
      this.$refs.workArrangeSettingAdd.initFormData(schedule)
      this.$refs.workArrangeSettingAdd.showDialog()
    },
    // 处理删除工作模式
    handleDeleteSchedule(schedule) {
      // 处理删除工作模式
      // 调用后端接口删除选中的工作模式
      // 弹窗
      this.$confirm('确定要删除该工作模式吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 确定删除
          this.deleteSchedule(schedule)
        })
        .catch(() => {
          // 取消删除
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 处理每页条数变化
    handleSizeChange(newSize) {
      this.pageSize = newSize
      this.fetchScheduleList() // 重新获取数据
    },
    // 处理页码变化
    handleCurrentChange(newPage) {
      this.currentPage = newPage
      this.fetchScheduleList() // 重新获取数据
    },
    // 获取排班列表数据
    fetchScheduleList() {
      const params = {
        page: this.currentPage,
        pageSize: this.pageSize
      }
      getWorkPattern(params)
        .then(res => {
          if (res.code === 200) {
            this.scheduleList = res.data.items.map(schedule => ({
              ...schedule,
              duration: ((schedule.workEndMinute1 - schedule.workStartMinute1) / 60).toFixed(2) + '小时' // 转换为小时并保留两位小数
            }))
            this.total = Number(res.data.counts)
          } else {
            this.$message.error(res.msg || '获取排班列表失败')
          }
        })
        .catch(error => {
          console.error('获取排班列表失败', error)
          this.$message.error('获取排班列表失败，请稍后重试')
        })
    }
  }
}
</script>
<style lang="scss" scoped>
.add-btn {
  margin-bottom: 20px;
}

.alert {
  margin: 10px 0px;
}

.pagination {
  margin-top: 40px;
  padding-bottom: 0px;
}

.el-select {
  width: 100%;
}

.car-models ::v-deep .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}
::v-deep .el-dialog__body {
  text-align: center;
  padding: 40px 60px 0 30px;
}

.warn-img {
  width: 40px;
}

p {
  height: 24px;
  font-size: 16px;
  font-family: PingFangSC, PingFangSC-Regular;
  font-weight: 400;
  color: #818693;
  line-height: 24px;
}

.el-form {
  ::v-deep .el-col {
    margin: 10px 0;
  }
}

.reset-btn {
  height: 40px !important;
}
.line {
  display: inline-block;
  width: 1px;
  height: 12px;
  background: #E4E7ED;
  margin: 0 10px;
}
::v-deep .el-pagination{
  text-align: center;
  margin-top: 20px;
}
</style>>
